import 'package:flutter/material.dart';

///普通按钮
class SimpleBtn extends StatefulWidget {
  final double height;
  final double width;
  final Widget child;
  final String text;
  final double radius;
  final Color color;
  final TextStyle style;
  final Function() onPressed;

  ///图标
  final Widget icon;

  ///icon是否在末尾
  final bool isIconEnd;

  ///图标与文字的间距
  final double iconSpece;

  ///内边距
  final EdgeInsetsGeometry padding;

  const SimpleBtn({
    Key key,
    this.height,
    this.child,
    this.width,
    @required this.text,
    this.radius,
    this.color,
    this.style,
    this.onPressed,
    this.icon,
    this.isIconEnd = false,
    this.iconSpece = 5,
    this.padding,
  }) : super(key: key);

  @override
  _SimpleBtnState createState() => _SimpleBtnState();
}

class _SimpleBtnState extends State<SimpleBtn> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: widget.padding == null ? widget.height ?? 60 : null,
      width: widget.width,
      child: FlatButton(
        padding: widget.padding,
        color: (widget.color ?? Theme.of(context).accentColor)
            .withOpacity(widget.onPressed == null ? 0.6 : 1),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(widget.radius ?? 10),
        ),
        materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
        onPressed: widget.onPressed,
        child: widget.child ??
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                if (widget.icon != null && !widget.isIconEnd)
                  Padding(
                    padding: EdgeInsets.only(right: widget.iconSpece),
                    child: widget.icon,
                  ),
                Text(
                  '${widget.text}',
                  style: widget.style ??
                      TextStyle(
                        color: Theme.of(context).bottomAppBarColor,
                        fontWeight: FontWeight.bold,
                        fontSize: 20,
                        height: 1,
                      ),
                ),
                if (widget.icon != null && widget.isIconEnd)
                  Padding(
                    padding: EdgeInsets.only(right: widget.iconSpece),
                    child: widget.icon,
                  ),
              ],
            ),
      ),
    );
  }
}
